<?php

/*
	任务列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['statuss']=$this->work_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>1);
	//类型。1表示开发任务，2表示bug修复任务，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	//任务名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_workliu_worklist',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$worker=pdo_get('jy_scrm_worker',array('id'=>$row['workerid'],'uniacid'=>$_SESSION['uniacid']));
		$row['member']=array('mobile'=>$worker['alias'],'avatar'=>$worker['avatar'],'nickname'=>$worker['name']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'任务列表'),
			array('op'=>'edit','title'=>'发布任务'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：这里的任务库，1、可以用来公司批量指派给员工；2、也可以让员工自己领取；3、也可以由客户选用任务发起！','delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'credit','title'=>'任务积分','type'=>'string'),
			array('name'=>'groupid','title'=>'任务分类','type'=>'select','options'=>'groups'),
			array('name'=>'receive_type','title'=>'领取方式','type'=>'radio','options'=>'receive_types'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'send','title'=>'发任务','show'=>array('receive_type'=>1)),
		)),
		'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'groupid','title'=>'任务分类','option_title'=>'title','options'=>'groups'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'任务名称'),
		),
	);
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('jy_workliu_workgroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['receive_types']=$this->receive_types;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_workliu_worklist',array('id'=>$id),array('groupid','title','content','status','image'));
	$item['image']=$item['image']?json_decode($item['image'],true):array();
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'任务详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'look'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
			array('type'=>'text','name'=>'content','title'=>'任务内容'),
			array('type'=>'images','name'=>'image','title'=>'截图'),
		),
	);
	exi($return);
}
//添加编辑任务
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'groupid'=>intval($post['groupid']),
			'content'=>trim($post['content']),
			'image'=>ijson_encode($post['image']),
			'credit'=>intval($post['credit']),
			'type'=>1,
			'receive_type'=>intval($post['receive_type']),
			'jiaji'=>intval($post['jiaji']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_workliu_worklist',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_workliu_worklist',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_workliu_worklist',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['groups']=pdo_getall('jy_workliu_workgroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回任务列表'),
		),
		'desc'=>'设置任务信息',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
			array('type'=>'switch','name'=>'jiaji','title'=>'是否加急'),
			array('type'=>'select','name'=>'groupid','title'=>'任务分类','options'=>'groups'),
			array('type'=>'radio','name'=>'receive_type','title'=>'指派类型','options'=>'receive_types'),
			array('type'=>'text','name'=>'content','title'=>'任务内容'),
			array('type'=>'images','name'=>'image','title'=>'任务截图'),
			array('type'=>'string','name'=>'credit','title'=>'任务积分'),
		),
	);
	$return['receive_types']=$this->receive_types;
	exi($return);
}
if($_RQ['op']=='send'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择任务！','error');
		}
		$item=pdo_get('jy_workliu_worklist',array('id'=>$id));
		$workerids=str_array($post['workerids']);
		if(!count($workerids)){
			exi('请先选择员工！','error');
		}
		foreach($workerids as $wv){
			if(!$wv){
				continue;
			}
			$data=array(
				'title'=>trim($item['title']),
				'groupid'=>intval($item['groupid']),
				'content'=>trim($item['content']),
				'image'=>ijson_encode($item['image']),
				'credit'=>intval($item['credit']),
				'type'=>0,
				'jiaji'=>intval($item['jiaji']),
				'status'=>1,
				'receive_type'=>1,
				'receive_time'=>TIMESTAMP,
				'workerid'=>intval($wv),
			);
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_workliu_worklist',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_workliu_worklist',array('id'=>$id),array('id','title'));
	if(!$item){
		exi('指定任务不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'指派员工处理任务',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称','read'=>true),
			array('type'=>'checkbox','title'=>'选择员工','name'=>'workerids','options'=>'workers'),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	foreach($return['workers'] as &$wv){
		$wv=$wv['name'];
	}
	exi($return);
}